<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>TxT数据请求</title>
	</head>
	<body>
		点击按钮获取info.txt的文本数据(同步请求) <br>
		<button type="button" onclick="fun1()">请求信息</button> <br>
		获得的信息：<span id="sp1"></span>
		<hr />
		点击按钮获取info.txt的文本数据(异步请求) <br>
		<button type="button" onclick="fun2()">请求信息</button> <br>
			获得的信息：<span id="sp2"></span>
			
			<script>
			
			function fun1(){
				var req=new XMLHttpRequest();
				req.open("GET","file/info.txt",false);
				req.send();	
				var info=req.responseText;
				document.getElementById("sp1").innerHTML=info;
				document.getElementById("sp1").style.color="red";
			}
			
			function fun2(){
				var req=new XMLHttpRequest();
				req.open("GET","file/info.txt",true);
				req.send();	
				req.onreadystatechange=function(){
					if(req.readyState==4&&req.status==200){
						var info=req.responseText;
						document.getElementById("sp2").innerHTML=info;
						document.getElementById("sp2").style.color="red";
					}
				}
				
			}
			</script>
	</body>
</html>
